package com.jianchi.fsp.appui;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.etiennelawlor.imagegallery.library.activities.ImageGalleryActivity;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class AllAppActivity extends AppCompatActivity {
    APPUIApplication app;
    List<History> histories;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_all_app);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        toolbar.setLogo(R.mipmap.ic_launcher);
        toolbar.setNavigationIcon(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        app = (APPUIApplication) getApplication(); // 获得CustomApplication对象
        histories=new ArrayList<>(app.appDic.keySet().size());
        for(String k : app.appDic.keySet()){
            History n=new History();
            n.viewTime=new Date();
            n.picUrl=app.appDic.get(k).get(0);
            n.appName=k;
            histories.add(n);
        }

        HistoryAdapter hadp = new HistoryAdapter(histories, getApplicationContext());

        ListView lv = (ListView) findViewById(R.id.listView);//得到ListView对象的引用 /*为ListView设置Adapter来绑定数据*/
        lv.setAdapter(hadp);

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                History h = (History) view.getTag();
                startAppGallery(h.appName);
            }
        });
    }

    private void startAppGallery(String appName){
        Intent intent = new Intent(AllAppActivity.this, ImageGalleryActivity.class);

        intent.putStringArrayListExtra("images", app.appDic.get(appName));
        intent.putExtra("title", appName);

        startActivity(intent);
    }
}
